package com.pi.pro.xcmg.am.was.web.cust.controller;

import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.wms.entity.TwaDocWaveHeader;
import com.pi.wms.entity.VwaDocWaveSoList;
import com.pi.wms.service.WaveService;
import com.pi.wms.service.WaveSoListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/outbound/wavesolist")
public class WaveSoController extends BaseCrudController<VwaDocWaveSoList, Long> {
    @Autowired
    private WaveSoListService service;
    @Autowired
    private WaveService headerService;

    @Override
    @GetMapping({"", "/{headerId}"})
    public String list(Model model, @PathVariable Map<?, ?> params, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        TwaDocWaveHeader header = this.getHeader(params);
        model.addAttribute("r", header);
        if (header != null) {
            Map<String, Object> maps = new HashMap();
            maps.put("m.headerId", header.getId());
            model.mergeAttributes(maps);
        }

        return super.list(model, params, request, redirectAttributes);
    }

    public WaveSoController() {
    }


    @Override
    protected ICrudService<VwaDocWaveSoList, Long> getCrudService() {
        return this.service;
    }

    @Override
    protected String getPrefix() {
        return "wavesolist";
    }

    @Override
    protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
        Long userId = SecurityUtils.getLoginUserId();
        query.put("userId", userId.toString());
        super.prepareDatatableConditions(query, input);
    }

    @Override
    protected VwaDocWaveSoList newModel(HttpServletRequest request) {
        return new VwaDocWaveSoList();
    }

    private TwaDocWaveHeader getHeader(HttpServletRequest request) {
        Object headerId = this.getParams(request).get("headerId");
        return this.getHeader(headerId);
    }

    private TwaDocWaveHeader getHeader(Map<?, ?> params) {
        Object headerId = params.get("headerId");
        return this.getHeader(headerId);
    }

    private TwaDocWaveHeader getHeader(Object headerId) {
        TwaDocWaveHeader header = new TwaDocWaveHeader();
        if (null != headerId && StringUtils.isNotBlank(headerId.toString())) {
            header = (TwaDocWaveHeader) this.headerService.getById(Long.valueOf(headerId.toString()));
        }

        return header;
    }


}
